package com.web;

import com.pojo.Book;
import com.pojo.Page;
import com.service.bookService;
import com.service.impl.bookServiceImpl;
import com.utils.webUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

public class bookServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    bookServlet b = new bookServlet();
    doPost(req,resp);
//    String action =  req.getParameter("action");
//    System.out.println("action===="+action);
//    Method method = null;
//    try {
//        method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//        method.invoke(this, req, resp);
//    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//        e.printStackTrace();
//    }
}
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        User u = new User();
//        try {
//            BeanUtils.populate(u,req.getParameterMap());
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        String action =  req.getParameter("action");
        Method method = null;
        try {
            method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
       int id = Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        bookService bookservice = new bookServiceImpl();
        bookservice.delBookById(id);
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
private void getBook(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException {
    int id = Integer.parseInt(req.getParameter("id"));
    System.out.println("--------------"+req.getParameter("pageNo"));
    bookService bookservice =new bookServiceImpl();
    Book book = bookservice.queryBookById(id);
    req.setAttribute("book",book);
    req.setAttribute("pageNo",req.getParameter("pageNo"));
    req.getRequestDispatcher("/项目/pages/manager/book_edit.jsp").forward(req,resp);
//    resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=list");

}
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(req.getParameter("id"));
            Book  book = new Book();
//        System.out.println("add");
//            for(Map.Entry<String,String[]> emtry:req.getParameterMap().entrySet()){
//            System.out.println(emtry.getKey()+"====="+ Arrays.toString(emtry.getValue()));
//            }
        //
            webUtils.copyParameTobean(book,req.getParameterMap());

            bookService bookservice = new bookServiceImpl();
            bookservice.addBook(book);
            resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("id"));
//        System.out.println("update");
     Book book = new Book();
       webUtils.copyParameTobean(book,req.getParameterMap());
       bookService bookservice = new bookServiceImpl();
        bookservice.updateBook(book);
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
        //全部显示
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        bookService book = new bookServiceImpl();

        req.setAttribute("book",book.queryBooks());
        req.getRequestDispatcher("/项目/pages/manager/book_manager.jsp").forward(req,resp);
    }
    //分页显示
    private void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        bookService book = new bookServiceImpl();
       String pageSize = req.getParameter("pageSize");
      int i =   webUtils.parseInt(pageSize,Page.PAGE_SIZE);
       String pageNo = req.getParameter("pageNo");
       int i1= webUtils.parseInt(pageNo,1);
       Page<Book> page =  book.pages(i1,i);
       page.setUrl("manager/bookServlet?action=page");
       req.setAttribute("page",page);
        req.getRequestDispatcher("/项目/pages/manager/book_manager.jsp").forward(req,resp);
    }
}
